
package org.come.test;

import com.gl.util.BuffeTest;
import org.come.bean.PathPoint;
//import org.come.init.CheckUpdate;
import org.come.login.ImageMap;
import org.come.login.LoginFrame;
import org.come.socket.GameClient;

import com.updateNew.MyIsif;

import java.io.*;
import java.util.HashMap;
import java.util.Scanner;

public class Main {
    public static LoginFrame frame;
    public static int index = 1;
    public static PathPoint pathPoint = new PathPoint(0, 0);
    public static String name = null;


    public static void main(String[] args) throws Exception {
        //获取界面风格
        MyIsif.setStyle(getStyle());
        //多开变单开
        String a = MyIsif.ifs;
        //加载图片

        if (a != null) {
            if (!a.equals("D")) {
                System.setProperty("java.awt.im.style", "on-the-spot");
                if (args != null) {
                    for (int i = 0; i < args.length; i++) {
                        if (args[i].startsWith("-name")) {
                            System.out.println(args[i]);
                            String[] v = args[i].split("=");
                            name = v[1];
                            continue;
                        }
                        if (args[i].startsWith("-index")) {
                            index = Integer.parseInt(args[i].split("=")[1]);
                            break;
                        }
                    }
                }
                if (index < 0) {
                    System.exit(0);
                }
                GameClient.potAndIpStrings = GameClient.getServerIpAndPort();
            }
        } else {

            String isd = getTypes();
            if (!isd.equals("Multiple")) {
                System.err.println("该程序不可以直接启动，请点击【登录器】启动！");
                System.exit(0);
            }
            System.setProperty("java.awt.im.style", "on-the-spot");
            if (args != null) {
                for (int i = 0; i < args.length; i++) {
                    if (args[i].startsWith("-name")) {
                        String[] v = args[i].split("=");
                        name = v[1];
                        continue;
                    }
                    if (args[i].startsWith("-index")) {
                        index = Integer.parseInt(args[i].split("=")[1]);
                        break;
                    }
                }
            }
            if (index < 0) {
                System.exit(0);
            }
            GameClient.potAndIpStrings = GameClient.getServerIpAndPort();
        }
        GameClient.potAndIpStrings = GameClient.getServerIpAndPort();
        frame = new LoginFrame();

//		Main.checkUpdate.setVisible(false);

        //多开变单开
        if (a != null) {
            if (!"D".equals(a)) {
                new Thread(() -> {
                    Scanner scanner = new Scanner(System.in);
                    while (true) {
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        String xy = scanner.next();
                        String[] xys = xy.split(":");
                        if (xys.length < 2) {
                            continue;
                        }
                        pathPoint.setX(Integer.parseInt(xys[0]));
                        pathPoint.setY(Integer.parseInt(xys[1]) + 64);
                    }
                }).start();
            }
        } else {
            new Thread(() -> {
                Scanner scanner = new Scanner(System.in);
                while (true) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    String xy = scanner.next();
                    String[] xys = xy.split(":");
                    if (xys.length < 2) {
                        continue;
                    }
                    pathPoint.setX(Integer.parseInt(xys[0]));
                    pathPoint.setY(Integer.parseInt(xys[1]) + 64);
                }
            }).start();
        }
    }




    public static boolean isMoreOpen() {
        // TODO Auto-generated method stub
        return false;
    }


    public static String getTypes() {
        String url = "";
        String ur = System.getProperty("user.dir");

        String filePath = ur + "/resource/other/StartUpMode.txt";
        FileInputStream fin = null;
        try {
            fin = new FileInputStream(filePath);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        InputStreamReader reader = new InputStreamReader(fin);
        BufferedReader buffReader = new BufferedReader(reader);
        String strTmp = "";
        try {
            while ((strTmp = buffReader.readLine()) != null) {
                url = strTmp;
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            buffReader.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return url;
    }


    public static String getStyle() {
        String url = "";
        String ur = System.getProperty("user.dir");

        String filePath = ur + "/resource/other/style.txt";
        FileInputStream fin = null;
        try {
            fin = new FileInputStream(filePath);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        InputStreamReader reader = new InputStreamReader(fin);
        BufferedReader buffReader = new BufferedReader(reader);
        String strTmp = "";
        try {
            while ((strTmp = buffReader.readLine()) != null) {
                url = strTmp;
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            buffReader.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return url;
    }


}